1. Field of the Invention
The present invention relates to compilers for programming languages. More specifically, the present invention relates to a method and an apparatus for selectively enabling expression folding during the compilation process by using a first assignment operator to specify an expression assignment and a second assignment operator to specify a value assignment.
2. Related Art
Rapid advances in computing technology make it possible to perform trillions of computational operations each second. This tremendous computational speed makes it practical to perform computationally intensive tasks as diverse as predicting the weather and optimizing the design of an aircraft engine. Such computational tasks are typically performed using machine-representable floating-point numbers to approximate values of real numbers. (For example, see the Institute of Electrical and Electronics Engineers (IEEE) standard 754 for binary floating-point numbers.)
In spite of their limitations, floating-point numbers are generally used to perform most computational tasks.
One limitation is that machine-representable floating-point numbers have a fixed-size word length, which limits their accuracy. Note that a floating-point number is typically encoded using a 32, 64 or 128-bit binary number, which means that there are only 232, 264 or 2128 possible symbols that can be used to specify a floating-point number. Hence, most real number values can only be approximated with a corresponding floating-point number. This creates estimation errors that can be magnified through even a few computations, thereby adversely affecting the accuracy of a computation.
A related limitation is that floating-point numbers contain no information about their accuracy. Most measured data values include some amount of error that arises from the measurement process itself. This error can often be quantified as an accuracy parameter, which can subsequently be used to determine the accuracy of a computation. However, floating-point numbers are not designed to keep track of accuracy information, whether from input data measurement errors or machine rounding errors. Hence, it is not possible to determine the accuracy of a computation by merely examining the floating-point number that results from the computation.
Interval arithmetic has been developed to solve the above-described problems. Interval arithmetic represents numbers as intervals specified by a first (left) endpoint and a second (right) endpoint. For example, the interval [a, b], where a less than b, is a closed, bounded subset of the real numbers, R, which includes a and b as well as all real numbers between a and b. Arithmetic operations on interval operands (interval arithmetic) are defined so that interval results always contain the entire set of possible values. The result is a mathematical system for rigorously bounding numerical errors from all sources, including measurement data errors, machine rounding errors and their interactions. (Note that the first endpoint normally contains the xe2x80x9cinfimumxe2x80x9d, which is the largest number that is less than or equal to each of a given set of real numbers. Similarly, the second endpoint normally contains the xe2x80x9csupremumxe2x80x9d, which is the smallest number that is greater than or equal to each of the given set of real numbers.)
However, computer systems are presently not designed to efficiently handle intervals and interval computations. Consequently, performing interval operations on a typical computer system can be hundreds of times slower than performing conventional floating-point operations. In addition, without a special representation for intervals, interval arithmetic operations fail to produce results that are as narrow as possible.
What is needed is a method and an apparatus for efficiently performing arithmetic operations on intervals with results that are as narrow as possible. (Interval results that are as narrow as possible are said to be xe2x80x9csharpxe2x80x9d.)
One problem in performing interval computations arises from optimizations that are often performed during the compilation process. One common optimization is xe2x80x9cexpression folding,xe2x80x9d in which a first expression is substituted for a variable within a second expression and the resulting second expression is simplified. For example, if a program includes the instruction X=A+B, followed by the instruction Z=Xxe2x88x92A, some compilers will substitute A+B for X in the expression for Z and will simplify, Z=(A+B)xe2x88x92A=B. As can be seen from this simple example, expression folding can potentially eliminate unnecessary computational operations.
However, expression folding can also create problems, especially for interval computations. For example, suppose a program must compute the summation of a number of intervals xi.   X  =            ∑              i        =        1            n        ⁢          xe2x80x83        ⁢          x      i      
Next, suppose that the program subsequently computes Yj=Xxe2x88x92xj. If the compiler performs expression folding by substituting the summation for X, and then simplifies to eliminate xj, the program ends up computing the following partial sum.       Y    j    =            ∑                        i          =          1                          i          ≠          j                    n        ⁢          xe2x80x83        ⁢          x      i      
Note that computing this partial sum is much slower than simply computing Yj=Xxe2x88x92xj, which simply involves performing a single subtraction operation. Hence, when possible, it is desirable not to use expression folding. However, note that if xj greater than  greater than Yj, expression folding prevents rounding errors caused by a large xj from undermining the accuracy of Yj. In this case it is desirable to use expression folding.
Hence, what is needed is a method and an apparatus that facilitates expression folding in cases where expression folding is advantageous, and that facilitates disabling expression folding in cases where expression folding is not advantageous.
One embodiment of the present invention provides a system that selectively enables expression folding during compilation of a program, wherein the compilation converts the program from source code into executable code. The system operates by forming an expression tree for an expression within the source code which includes an assignment operator. If the assignment operator is a first assignment operator that is a value assignment, only the computed value can be used in subsequent expressions, thereby disabling expression folding during the compilation process. On the other hand, if the assignment operator is a second assignment operator that specifies an expression assignment, the entire expression can be used in place of the variable on the left of the expression assignment, thereby enabling expression folding during the compilation process.
In one embodiment of the present invention, the expression includes a mathematical interval.
In one embodiment of the present invention, the expression folding involves substituting of a first expression for a variable within a second expression, and then simplifying the resulting second expression through mathematically-equivalent symbolic manipulation.
In one embodiment of the present invention, the first assignment operator is represented as xe2x80x9c:=xe2x80x9d within the source code.
In one embodiment of the present invention, the second assignment operator is represented as xe2x80x9c=xe2x80x9d within the source code.
In one embodiment of the present invention, if the assignment operator is an expression assignment operator, expression folding is required if the expression on the right-hand-side of the expression assignment operator contains a non-degenerate literal interval constant.